/*
 * 作者: 侯晓宇
 * 时间: 2017-01-11
 * 说明：
 * Copyright (c) 2016-2017 中研沃兮
 */
define(function(require) {
	require("jquery");
	var appInfo = require('app/AppInfo');
	var c = require('app/utils/CommonUtils');
	var hc = require('app/utils/HttpClientUtils');
	var tip = require('app/utils/TipUtils');
	var dbu = require('app/utils/DBUtils');
	var UserInfoUtils = require('app/utils/UserInfoUtils');
	var EventUtils = require('app/utils/EventUtils');
	var APIUtils = require('app/utils/APIUtils');

	var win = window;

	var action = {};
	var tabBar_api = api.require('NVTabBar');

	win.navbars = [];
	win.nav_now = 1;
	//默认显示的tab
	win.tab_now = 0;
	win.header_h = 0;
	win.footer_h = 0;
	win.sidebar_show = false;
	win.nowindex = 0;

	action.setHeaderH = function(h) {
		win.header_h = h;
	}
	action.setFooterH = function(h) {
		win.footer_h = h;
	}
	action.showHeader = function() {

		api.setScreenOrientation({
			orientation : 'portrait_up'
		});

		api.openFrame({
			name : 'HomePageHeaderFrm',
			url : './HomePageHeaderFrm.html',
			//pageParam:param,
			rect : {
				x : 0,
				y : 0,
				w : APIUtils.winW(),
				h : win.header_h
			},
			bgColor : 'rgba(0,0,0,0)'
		});

	}
	action.openHomeFrm = function(reload) {

		api.setScreenOrientation({
			orientation : 'portrait_up'
		});

		action.showHeader();

		var name = "IndexHomePageFrm";
		var _reload = false;//true

		if (!c.isNull(reload)) {
			_reload = reload;
		}

		api.openFrame({
			name : name,
			url : './' + name + '.html',
			bounces : true,
			rect : {
				x : 0,
				y : win.header_h,
				w : APIUtils.winW(),
				h : APIUtils.winH() - win.footer_h - win.header_h
			},
			reload : _reload
		});

	}
	action.generateNavbar = function(name, uuid, num) {
		navbars.push({
			title : name,
			bg : '#FFFFFF',
			bgSelected : '#FFFFFF',
			//自定义数据
			uuid : uuid,
			num : num
		});
	};

	action.openNavbar = function() {
		var that = this;
		var h = win.header_h;
		var NVNavigationBar = api.require('NVNavigationBar');
		NVNavigationBar.open({
			rect : {
				x : h,
				y : 0,
				w : api.frameWidth - h,
				h : h
			},
			styles : {
				orientation : 'horizontal',
				bg : '#ffffff',
				bgAlpha : 1,
				font : {
					size : 14,
					sizeSelected : 18,
					color : 'rgb(60,60,60)',
					colorSelected : '#ff6a32',
					alpha : 1
				},
				itemSize : {
					w : 55,
					h : h
				}
			},
			items : navbars,
			selectedIndex : 1,
			fixedOn : api.frameName,
			id : 'navigationBar'
		}, function(ret, err) {
			if (ret.eventType == "click") {
				action.openCatalogByIndex(ret.index);
			} else {
				action.openCatalogByIndex(1);
			}
		});

	}
	action.openCatalogByUUID = function(cataloguuid, reload) {

		var name = "IndexCatalogFrm";
		var showBanner = false;
		var _reload = true;
		if (c.isNull(reload)) {
			_reload = true
		} else {
			_reload = reload;
		}

		if (appInfo.uuid_catalog_follow == cataloguuid) {
			if (!UserInfoUtils.isLogined()) {
				//未登陆则打开LoginWin.js
				api.openWin({
					name : 'LoginAndRegistWin',
					url : './LoginAndRegistWin.html'
				});
				return;
			}
		}

		if (navbars[1].uuid == cataloguuid) {
			showBanner = true;
		}

		api.openFrame({
			name : name,
			url : './' + name + '.html',
			bounces : true,
			rect : {
				x : 0,
				y : win.header_h + 1,
				w : APIUtils.winW(),
				h : APIUtils.winH() - win.header_h - win.footer_h - 1
			},
			pageParam : {
				catalogUuid : cataloguuid,
				showBanner : showBanner
			},
			reload : _reload
		});

	}
	action.openCatalogByIndex = function(index, reload) {
		var uuid = navbars[index].uuid;
		action.openCatalogByUUID(uuid, reload);
	}
	action.getCatalogs = function() {
		var that = this;
		var promise = new Promise(function(resolve, reject) {

			hc.post(appInfo.serverbase + "/wjt/catalogs_app", {
				appid : appInfo.appid
			}, function(resp) {
				if (resp.status != "200") {
					//					reject(resp.result);
					return;
				}
				//成功
				resolve(resp.result);

			});
			//--end post
		});
		return promise;
	}
	
	
	action.launchSelectLiveType = function(){
	////////-begin
		api.actionSheet({
			title : '选择直播间类型',
			cancelTitle : '关闭',
			buttons : ['视频直播', 'PPT语音直播']
		}, function(ret, err) {
			var index = ret.buttonIndex;
			if (index == 2) {

				//直播开始
				var name = 'PPTAutoCreateRoomWin';
				api.openWin({
					name : name,
					url : './' + name + '.html',
					bounces : false,
					reload : true,
					animation : {
						type : "reveal", //动画类型（详见动画类型常量）
						subType : "from_bottom", //动画子类型（详见动画子类型常量）
						duration : 300 //动画过渡时间，默认300毫秒
					}
				});

			} else if (index == 1) {

				//直播开始
				var name = 'CreateRoomSimpleWin';
				api.openWin({
					name : name,
					url : './' + name + '.html',
					bounces : false,
					reload : true,
					animation : {
						type : "reveal", //动画类型（详见动画类型常量）
						subType : "from_bottom", //动画子类型（详见动画子类型常量）
						duration : 300 //动画过渡时间，默认300毫秒
					}
				});
			}
		});
		////////-end										
	}

	action.openTab = function() {

		tabBar_api.open({
			styles : {
				bg : '#fff',
				h : 50,
				dividingLine : {
					width : 0.5,
					color : 'rgb(237,116,55)'
				},
				badge : {
					bgColor : 'rgb(237,116,55)',
					numColor : '#fff',
					size : 6.0
				}
			},
			items : [{
				w : api.winWidth / 3.0,
				bg : {
					marginB : 0,
					image : 'rgba(0,0,0,0)'
				},
				iconRect : {
					w : 25.0,
					h : 25.0,
				},
				icon : {
					normal : 'widget://res/tabbar/home.png',
					highlight : 'widget://res/tabbar/home-active.png',
					selected : 'widget://res/tabbar/home-active.png'
				},
				title : {
					text : '主页',
					size : 12.0,
					normal : '#696969',
					selected : 'rgb(237,116,55)',
					marginB : 4.0
				}
			}, {
				w : api.winWidth / 3.0,
				bg : {
					marginB : 0,
					image : 'rgba(0,0,0,0)'
				},
				iconRect : {
					w : 49.0,
					h : 49.0
				},
				icon : {
					normal : 'widget://res/tabbar/zhibo_button.png',
					highlight : 'widget://res/tabbar/zhibo_button.png',
					selected : 'widget://res/tabbar/zhibo_button.png'
				},
				title : {
					text : '',
					size : 0,
					normal : '#696969',
					selected : 'rgb(237,116,55)',
					marginB : 0
				}

			}, {
				w : api.winWidth / 3.0,
				bg : {
					marginB : 0,
					image : 'rgba(0,0,0,0)'
				},
				iconRect : {
					w : 25.0,
					h : 25.0,
				},
				icon : {
					normal : 'widget://res/tabbar/user.png',
					highlight : 'widget://res/tabbar/user-active.png',
					selected : 'widget://res/tabbar/user-active.png'
				},
				title : {
					text : '我',
					size : 12.0,
					normal : '#696969',
					selected : 'rgb(237,116,55)',
					marginB : 4.0
				}
			}],
			selectedIndex : 0
		}, function(ret, err) {
			if (ret.eventType == "show") {
				action.openHomeFrm();

			} else if (ret.eventType == "click") {
				win.tab_now = ret.index;

				api.setScreenOrientation({
					orientation : 'portrait_up'
				});

				win.nowindex = ret.index

				if (ret.index == 0) {
					api.closeFrame({
						name : 'IndexMeFrm'
					});
					action.openHomeFrm();

				} else if (ret.index == 1) {

					if (!UserInfoUtils.isLogined()) {
						//未登陆则打开LoginWin.js
						api.openWin({
							name : 'LoginAndRegistWin',
							url : './LoginAndRegistWin.html'
						});

						tabBar_api.setSelect({
							index : 0
						});
						win.nowindex = 0;
						return;
					}

					if (c.isNullStr(UserInfoUtils.getLoginedUser().usertel)) {

						api.confirm({
							title : '请完善您的个人信息',
							msg : '创建直播间需要获取管理员授权，是否去完善您的信息',
							buttons : ['暂不', '去完善']
						}, function(ret, err) {
							//coding...
							if (ret.buttonIndex == "2") {

								api.openWin({
									name : 'CompleteUserInfoWin',
									url : './CompleteUserInfoWin.html',
									bounces : false,
									reload : true
								});

							}
						});

					} else {
					
						
					
						var url = appInfo.serverbase + "/luyan/user/"+UserInfoUtils.getLoginedUser().userid;
						var body = {};
						hc.post(url,body,function(resp){
						
							if(resp.status == "200"){
							
								if(resp.result.next){
									var user = {};
									user.uuid = resp.result.useruuid;
									user.wxUnionid = resp.result.unionid;
									user.tel = resp.result.tel;
									user.name = resp.result.name;
									user.wxHeadimgurl = resp.result.headimgurl;
									user.userType = resp.result.usertype;
							
							
									UserInfoUtils.saveUser(user);	

									if(!c.isNullStr(user.userType)){
										action.launchSelectLiveType();
									}else{
										tip.show("您还没有获得直播授权，请您主动搜索微信公众号：“好教授在线”，点击“合作”联系我们。");
									}		
								}
							}
							
						});
						

					}

				} else if (ret.index == 2) {

					if (!UserInfoUtils.isLogined()) {
						//未登陆则打开LoginWin.js
						api.openWin({
							name : 'LoginAndRegistWin',
							url : './LoginAndRegistWin.html'
						});

						tabBar_api.setSelect({
							index : 0
						});
						win.nowindex = 0;
						return;
					}
					//我
					api.openFrame({
						rect : {
							x : 0,
							y : 0,
							w : APIUtils.winW(),
							h : APIUtils.winH() - win.footer_h
						},
						name : 'IndexMeFrm',
						url : './IndexMeFrm.html',
						bounces : false,
						reload : false,
					});

				}
			}
			//			openTabBar_callback(ret);
		});

	}

	action.listenReset = function() {

		EventUtils.listen(api.winName, 'reset', false, function() {
			api.closeToWin({
				name : 'root'
			});
			api.closeFrame({
				name : 'IndexMeFrm'
			});
			tabBar_api.setSelect({
				index : 0
			});
			win.nowindex = 0;

			action.openHomeFrm();

		});

		EventUtils.listen(api.winName, 'reset_me', false, function() {
			api.closeToWin({
				name : 'root'
			});
			//我
			api.openFrame({
				rect : {
					x : 0,
					y : 0,
					w : APIUtils.winW(),
					h : APIUtils.winH() - win.footer_h
				},
				name : 'IndexMeFrm',
				url : './IndexMeFrm.html',
				bounces : false,
				reload : false,
			});
			tabBar_api.setSelect({
				index : 2
			});
			win.nowindex = 2;
		});
	}

	action.listenSidebar = function() {
		EventUtils.listen('root', 'homepageSidebar_status', false, function(sidebar_show) {
			win.sidebar_show = sidebar_show;
		});
	}

	action.listenKeyback = function() {
		api.addEventListener({
			name : 'keyback'
		}, function(ret, err) {

			if (win.nowindex == 0) {
				if (win.sidebar_show) {
					api.closeFrame({
						name : 'HomePageSidebarFrm'
					});
					win.sidebar_show = false;
				} else {
					api.closeWin({
					});
				}

			} else {
				EventUtils.send('root', 'reset');
			}

		});
	}

	return action;
}); 